<?php
/**
 * Created by PhpStorm.
 * User: osxfal
 * Date: 2019/2/23
 * Time: 下午8:01
 */
namespace app\index\controller;

use think\Controller;

/* 管理员管理*/
class AdminManage extends Controller{

    //判断用户是否登录
    /*public function __construct()
    {
        parent::__construct();
        if(empty(session('user')))
        {
            $this->error('对不起,您还没有登录!请先登录!','index/login');


        }

    }*/

    /*管理员列表

        表： go_admin  条件 mid = 0
    */
    public function adminList(){
        //页码参数
        $page = input('get.page');
        $lines = input('get.lines');

        empty($page) && $page = 1;
        empty($lines) && $lines = 20;

        $limit = ($page - 1) * $lines . ',' . $lines;

        $res = db('admin')->where(['mid'=>0])->limit($limit)->order('uid asc')->select();

        foreach($res as $k=>&$v){
            if($v['addtime']){//推送时间
                $v['addtime'] = date('Y-m-d H:i:s',$v['addtime']);
            }

            if($v['logintime']){//推送时间
                $v['logintime'] = date('Y-m-d H:i:s',$v['logintime']);
            }

        }


        $total = db('admin')->field('COUNT(uid) as t')->find();

        $msg = [];
        if(!empty($res) && $total['t']!==false){
            $msg['rows'] = $res;
            $msg['total'] = $total['t'];
            $msg['result'] = 1;
            $msg['tips'] = '查询成功！';
            $msgList = json_encode($msg,320);
        }
        elseif(empty($res)){
            $msg['rows'] = [];
            $msg['total'] = 0;
            $msg['result'] = 1;
            $msg['tips'] = '查询成功！并没有匹配的数据哟~~';
            $msgList = json_encode($msg,320);
        }else{

            $msg['result'] = 2;
            $msg['tips'] = '网络忙~稍后重试';
            $msgList = json_encode($msg,320);
        }


        exit($msgList);
    }

    /*管理员添加*/
    public function adminAdd(){
        $username = input('post.username');
        $userpass = input('post.userpass');
        $c_userpass = input('post.c_userpass');//确认密码
        $email = input('post.email');//email

        //渠道商下拉菜单 写死一个。 不用处理

        if($userpass == $c_userpass){
            $data = [];
            $data['username'] = trim($username);
            $data['userpass'] = md5(trim($userpass));
            $data['useremail'] = trim($email);
            $data['mid'] = 0; //管理员都是 0  写si

            $res = db('admin')->insert($data);
            $box = []; //初始化 消息盒子 用来将结果信息返回给前端
            if($res){
                $box['result'] =1;
                $box['tips'] = '成功添加渠道商';

                $box = json_encode($box,320);
                exit($box);
                // $this->success('添加渠道商成功！',url('distributorList'));
            }else{
                // $this->error('添加渠道商失败！');
                $box['result'] =2;
                $box['tips'] = '添加渠道商失败!';
                $box = json_encode($box,320);
                exit($box);
            }
        }
        else{

            $box['result'] =2;
            $box['tips'] = '两次输入密码不一样！!';
            $box = json_encode($box,320);
            exit($box);
            // $this->error('两次输入密码不一样！');
        }
    }



    /*管理员修改,编辑
        go_admin;   条件 mid=0
    */
    public function edit_admin(){

    }


    /*管理员删除
        软删
    */
    public function admin_del(){
        $uid = input('post.id');
        db('admin')->delete(['uid'=>$uid]);
        $msg['result'] = 1;
        $msg['tips'] = '删除成功！';
        $msgList = json_encode($msg,320);
        exit($msgList);

    }
}